// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.core.process;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class CompProcessHelper {

    private static String SEP = "_"; //$NON-NLS-1$

    private static int i = 0;

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "generateNewCompNodeName".
     * 
     * @param process
     * @param compName
     * @return
     */
    public static String generateNewCompNodeName(ICompProcess process, String compName) {
        i = 0;
        compName = compName + SEP;
        List<String> list = new ArrayList<String>();
        List<? extends ICompNode> compNodes = process.getCompNodes();
        for (ICompNode compNode : compNodes) {
            String uniqueName = compNode.getUniqueName();
            if (uniqueName != null && uniqueName.startsWith(compName)) {
                list.add(uniqueName);
            }
        }
        Collections.sort(list);
        for (String columnName : list) {
            String num = columnName.substring(compName.length());
            if (num.equals((new Integer(i)).toString())) {
                i++;
            }
        }
        return compName + i;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "generateNewCompConnectionName".
     * 
     * @param process
     * @param connName
     * @return
     */
    public static String generateNewCompConnectionName(ICompProcess process, String connName) {
        i = 0;
        connName = connName + SEP;
        List<String> list = new ArrayList<String>();
        List<? extends ICompConnection> conns = process.getConnections();
        for (ICompConnection conn : conns) {
            String uniqueName = conn.getName();
            if (uniqueName != null && uniqueName.startsWith(connName)) {
                list.add(uniqueName);
            }
        }
        Collections.sort(list);
        for (String columnName : list) {
            String num = columnName.substring(connName.length());
            if (num.equals((new Integer(i)).toString())) {
                i++;
            }
        }
        return connName + i;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "generateElementNewLocation".
     * 
     * @param process
     * @param elem
     * @param rectangle
     * @return
     */
    public static Point generateElementNewLocation(ICompProcess process, CompElement elem, Rectangle rectangle) {
        Point newLocation = rectangle.getLocation().getCopy();
        for (CompElement e : process.getElements()) {
            if (e != elem && e.getLocation() != null && e.getSize() != null && !e.getSize().equals(Dimension.SINGLETON)) {
                Rectangle t = new Rectangle(e.getLocation(), e.getSize());
                if (rectangle.touches(t)) {
                    return generateElementNewLocation(process, e,
                            new Rectangle(t.getBottomRight(), rectangle.getSize().getCopy()));
                }
            }
        }
        return newLocation;
    }
}
